<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>Java过渡Kotlin04：类和对象</title>
  
  
  <meta property="og:title" content="Java过渡Kotlin04：类和对象">
  
  
  <meta property="og:url" content="http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin04%EF%BC%9A%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2020-03-02">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Kotlin">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      Java过渡Kotlin04：类和对象
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2020-03-02T04:09:01.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2020-03-02</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Kotlin/" class="post-meta-link">Kotlin</a>
    
    
    
    <span class="dot"></span>
    <span>1.8k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Kotlin/" class="post-meta-link">Kotlin</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <h1 id="1-定义"><a href="#1-定义" class="headerlink" title="1.定义"></a>1.定义</h1><p>Kotlin 中使用关键字 class 声明类，后面紧跟类名：</p>
<pre><code>class Runoob &#123;  // 类名为 Runoob
    // 大括号内是类体构成
&#125;
</code></pre>
<p>空类</p>
<pre><code>class Empty
</code></pre>
<p>成员函数</p>
<p>class Runoob() {<br>    fun foo() { print(“Foo”) } // 成员函数<br>}</p>
<h1 id="2-类的属性"><a href="#2-类的属性" class="headerlink" title="2.类的属性"></a>2.类的属性</h1><p>var 声明 可变变量<br>val 声明 常量、不可变、尽可赋值一次</p>
<pre><code>class Runoob &#123;
    var name: String = ……
    var url: String = ……
    var city: String = ……
&#125;

val site = Runoob() // Kotlin 中没有 new 关键字

site.name           // 使用 . 号来引用
site.url
</code></pre>
<p>Koltin 中的类可以有一个 主构造器，以及一个或多个次构造器，主构造器是类头部的一部分，位于类名称之后:</p>
<pre><code>class Person constructor(firstName: String) &#123;&#125;
</code></pre>
<p>如果主构造器没有任何注解，也没有任何可见度修饰符，那么constructor关键字可以省略</p>
<pre><code>class Person(firstName: String) &#123;
&#125;
</code></pre>
<h1 id="3-getter-和-setter"><a href="#3-getter-和-setter" class="headerlink" title="3.getter 和 setter"></a>3.getter 和 setter</h1><p>属性声明的完整语法：</p>
<pre><code>var &lt;propertyName&gt;[: &lt;PropertyType&gt;] [= &lt;property_initializer&gt;]
    [&lt;getter&gt;]
    [&lt;setter&gt;]
</code></pre>
<p>getter 和 setter 都是可选</p>
<p>如果属性类型可以从初始化语句或者类的成员函数中推断出来，那就可以省去类型，val不允许设置setter函数，因为它是只读的。</p>
<p>实例：</p>
<pre><code>class Person &#123;

    var lastName: String = &quot;zhang&quot;
        get() = field.toUpperCase()   // 将变量赋值后转换为大写
        set

    var no: Int = 100
        get() = field                // 后端变量
        set(value) &#123;
            if (value &lt; 10) &#123;       // 如果传入的值小于 10 返回该值
                field = value
            &#125; else &#123;
                field = -1         // 如果传入的值大于等于 10 返回 -1
            &#125;
        &#125;

    var heiht: Float = 145.4f
        private set
&#125;

// 测试
fun main(args: Array&lt;String&gt;) &#123;
    var person: Person = Person()

    person.lastName = &quot;wang&quot;

    println(&quot;lastName:$&#123;person.lastName&#125;&quot;)

    person.no = 9
    println(&quot;no:$&#123;person.no&#125;&quot;)

    person.no = 20
    println(&quot;no:$&#123;person.no&#125;&quot;)

&#125;
</code></pre>
<h1 id="4-主构造器"><a href="#4-主构造器" class="headerlink" title="4. 主构造器"></a>4. 主构造器</h1><p>主构造器中不能包含任何代码，初始化代码可以放在初始化代码段中，初始化代码段使用 init 关键字作为前缀。</p>
<pre><code>class Person constructor(firstName: String) &#123;
    init &#123;
        println(&quot;FirstName is $firstName&quot;)
    &#125;
&#125;
</code></pre>
<p>主构造器的参数可以在初始化代码段中使用，也可以在类主体n定义的属性初始化代码中使用。 一种简洁语法，可以通过主构造器来定义属性并初始化属性值（可以是var或val）</p>
<pre><code>class People(val firstName: String, val lastName: String) &#123;
    //...
&#125;   
</code></pre>
<p>如果构造器有注解，或者有可见度修饰符，这时constructor关键字是必须的，注解和修饰符要放在它之前</p>
<pre><code>class Runoob  constructor(name: String) &#123;  // 类名为 Runoob
    // 大括号内是类体构成
    var url: String = &quot;http://www.runoob.com&quot;
    var country: String = &quot;CN&quot;
    var siteName = name

    init &#123;
        println(&quot;初始化网站名: $&#123;name&#125;&quot;)
    &#125;

    fun printTest() &#123;
        println(&quot;我是类的函数&quot;)
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val runoob =  Runoob(&quot;菜鸟教程&quot;)
    println(runoob.siteName)
    println(runoob.url)
    println(runoob.country)
    runoob.printTest()
    
    //初始化网站名: 菜鸟教程
    //菜鸟教程
    //http://www.runoob.com
    //CN
    //我是类的函数
&#125;
</code></pre>
<h1 id="5-次构造器"><a href="#5-次构造器" class="headerlink" title="5.次构造器"></a>5.次构造器</h1><p>类也可以有二级构造函数，需要加前缀 constructor:</p>
<pre><code> class Person &#123; 
    constructor(parent: Person) &#123;
        parent.children.add(this) 
    &#125;
&#125;
</code></pre>
<p>如果类有主构造函数，每个次构造函数都要，或直接或间接通过另一个次构造函数代理主构造函数。在同一个类中代理另一个构造函数使用 this 关键字</p>
<pre><code>class Person(val name: String) &#123;
    constructor (name: String, age:Int) : this(name) &#123;
        // 初始化...
    &#125;
&#125;
</code></pre>
<p>如果一个非抽象类没有声明构造函数(主构造函数或次构造函数)，它会产生一个没有参数的构造函数。构造函数是 public 。如果你不想你的类有公共的构造函数，你就得声明一个空的主构造函数</p>
<pre><code>class DontCreateMe private constructor () &#123;
&#125;
</code></pre>
<p>实例</p>
<pre><code>class Runoob  constructor(name: String) &#123;  // 类名为 Runoob
    // 大括号内是类体构成
    var url: String = &quot;http://www.runoob.com&quot;
    var country: String = &quot;CN&quot;
    var siteName = name

    init &#123;
        println(&quot;初始化网站名: $&#123;name&#125;&quot;)
    &#125;
    // 次构造函数
    constructor (name: String, alexa: Int) : this(name) &#123;
        println(&quot;Alexa 排名 $alexa&quot;)
    &#125;

    fun printTest() &#123;
        println(&quot;我是类的函数&quot;)
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val runoob =  Runoob(&quot;菜鸟教程&quot;, 10000)
    println(runoob.siteName)
    println(runoob.url)
    println(runoob.country)
    runoob.printTest()
&#125;
//result
初始化网站名: 菜鸟教程
Alexa 排名 10000
菜鸟教程
http://www.runoob.com
CN
我是类的函数
</code></pre>
<h1 id="6-抽象类"><a href="#6-抽象类" class="headerlink" title="6.抽象类"></a>6.抽象类</h1><p>抽象是面向对象编程的特征之一，类本身，或类中的部分成员，都可以声明为abstract的。抽象成员在类中不存在具体的实现。</p>
<p>注意：无需对抽象类或抽象成员标注open注解。</p>
<pre><code>open class Base &#123;
    open fun f() &#123;&#125;
&#125;

abstract class Derived : Base() &#123;
    override abstract fun f()
&#125;
</code></pre>
<h1 id="7-嵌套类"><a href="#7-嵌套类" class="headerlink" title="7.嵌套类"></a>7.嵌套类</h1><pre><code>class Outer &#123;                  // 外部类
    private val bar: Int = 1
    class Nested &#123;             // 嵌套类
        fun foo() = 2
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val demo = Outer.Nested().foo() // 调用格式：外部类.嵌套类.嵌套类方法/属性
    println(demo)    // == 2
&#125;、
</code></pre>
<p>​    </p>
<h1 id="8-内部类"><a href="#8-内部类" class="headerlink" title="8.内部类"></a>8.内部类</h1><p>内部类使用 inner 关键字来表示。</p>
<p>内部类会带有一个对外部类的对象的引用，所以内部类可以访问外部类成员属性和成员函数</p>
<p>为了消除歧义，要访问来自外部作用域的 this，我们使用this@label，其中 @label 是一个 代指 this 来源的标签</p>
<pre><code>class Outer &#123;
    private val bar: Int = 1
    var v = &quot;成员属性&quot;
    /**嵌套内部类**/
    inner class Inner &#123;
        fun foo() = bar  // 访问外部类成员
        fun innerTest() &#123;
            var o = this@Outer //获取外部类的成员变量
            println(&quot;内部类可以引用外部类的成员，例如：&quot; + o.v)
        &#125;
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val demo = Outer().Inner().foo()
    println(demo) //   1
    val demo2 = Outer().Inner().innerTest()   
    println(demo2)   // 内部类可以引用外部类的成员，例如：成员属性
&#125;
</code></pre>
<h1 id="9-匿名内部类"><a href="#9-匿名内部类" class="headerlink" title="9.匿名内部类"></a>9.匿名内部类</h1><pre><code>class Test &#123;
    var v = &quot;成员属性&quot;

    fun setInterFace(test: TestInterFace) &#123;
        test.test()
    &#125;
&#125;

/**
 * 定义接口
 */
interface TestInterFace &#123;
    fun test()
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    var test = Test()

    /**
     * 采用对象表达式来创建接口对象，即匿名内部类的实例。
      
      object : TestInterFace，这个 object 是 Kotlin 的关键字，要实现匿名内部类，就必须使用 object 关键字，不能随意替换其它单词，切记切记。
      
     */
    test.setInterFace(object : TestInterFace &#123;
        override fun test() &#123;
            println(&quot;对象表达式创建匿名内部类的实例&quot;)
        &#125;
    &#125;)
&#125;
</code></pre>
<h1 id="10-类的修饰符"><a href="#10-类的修饰符" class="headerlink" title="10.类的修饰符"></a>10.类的修饰符</h1><pre><code>abstract    // 抽象类  
final       // 类不可继承，默认属性
enum        // 枚举类
open        // 类可继承，类默认是final的
annotation  // 注解类

private    // 仅在同一个文件中可见
protected  // 同一个文件中或子类可见
public     // 所有调用的地方都可见
internal   // 同一个模块中可见
</code></pre>
<p>​    </p>
<h1 id="11-注意："><a href="#11-注意：" class="headerlink" title="11.注意："></a>11.注意：</h1><p>1）创建对象的区别</p>
<pre><code>var demo = Outter.Nested()// 嵌套类，Outter后边没有括号
var demo = Outter().Inner();// 内部类，Outter后边有括号
</code></pre>
<p>也就是说，要想构造内部类的对象，必须先构造外部类的对象，而嵌套类则不需要；</p>
<p>获取实体类的所有属性</p>
<pre><code>class Runoob &#123;
    var name: String = ……
    var url: String = ……
    var city: String = ……
&#125;

var entity = Runoob()
entity.javaClass.getDeclaredFields().forEach &#123; field -&gt; println(field.name) &#125;
</code></pre>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin04%EF%BC%9A%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1/">http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin04%EF%BC%9A%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2020/03/02/Java过渡Kotlin05：继承和接口/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">Java过渡Kotlin05：继承和接口 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2020/03/01/Java过渡Kotlin03：流程控制/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">Java过渡Kotlin03：流程控制 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%AE%9A%E4%B9%89"><span class="toc-text">1.定义</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E7%B1%BB%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">2.类的属性</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-getter-%E5%92%8C-setter"><span class="toc-text">3.getter 和 setter</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%B8%BB%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">4. 主构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%AC%A1%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">5.次构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-text">6.抽象类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E5%B5%8C%E5%A5%97%E7%B1%BB"><span class="toc-text">7.嵌套类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">8.内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-%E5%8C%BF%E5%90%8D%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">9.匿名内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E7%B1%BB%E7%9A%84%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-text">10.类的修饰符</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11-%E6%B3%A8%E6%84%8F%EF%BC%9A"><span class="toc-text">11.注意：</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%AE%9A%E4%B9%89"><span class="toc-text">1.定义</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E7%B1%BB%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">2.类的属性</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-getter-%E5%92%8C-setter"><span class="toc-text">3.getter 和 setter</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%B8%BB%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">4. 主构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%AC%A1%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">5.次构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-text">6.抽象类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E5%B5%8C%E5%A5%97%E7%B1%BB"><span class="toc-text">7.嵌套类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">8.内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-%E5%8C%BF%E5%90%8D%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">9.匿名内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E7%B1%BB%E7%9A%84%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-text">10.类的修饰符</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11-%E6%B3%A8%E6%84%8F%EF%BC%9A"><span class="toc-text">11.注意：</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%AE%9A%E4%B9%89"><span class="toc-text">1.定义</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E7%B1%BB%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">2.类的属性</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-getter-%E5%92%8C-setter"><span class="toc-text">3.getter 和 setter</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%B8%BB%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">4. 主构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%AC%A1%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-text">5.次构造器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-text">6.抽象类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E5%B5%8C%E5%A5%97%E7%B1%BB"><span class="toc-text">7.嵌套类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">8.内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-%E5%8C%BF%E5%90%8D%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-text">9.匿名内部类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E7%B1%BB%E7%9A%84%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-text">10.类的修饰符</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11-%E6%B3%A8%E6%84%8F%EF%BC%9A"><span class="toc-text">11.注意：</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>